package DAO;

import POJO.Enseignement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class EnseignementDAO extends DAO<Enseignement>{

    public EnseignementDAO(Connection c){
        super(c);
    }

    @Override
    public Enseignement create(Enseignement obj) {
        PreparedStatement prep = null;
        Enseignement e = null;
        ResultSet result = null;
    	try{
            prep = this.prepare("INSERT INTO Enseignement (Enseignement_ID, Master_ID, intitule, description)" +
				" VALUES (nextval('enseignement_enseignement_id_seq'),?,?,?)");
            prep.setLong(1,obj.getMaster().getId());
            prep.setString(2,obj.getIntitule());
            prep.setString(3, obj.getDescription());
            prep.execute();
			prep = prepare("SELECT currval('enseignement_enseignement_id_seq') as id");
			if(prep.execute()){
			  result = prep.getResultSet();
			}
			while(result.next()){
				long id = result.getLong("id");
				e = obj; e.setId(id);
			}
    	} catch (SQLException sqle){
    		sqle.printStackTrace();
    	}finally{
            try{
                if(prep!=null){
                    prep.close();
                }
            }catch(SQLException ex){
                ex.printStackTrace();
            }
        }
    	return e;
    }
    
    @Override
    public boolean update(Enseignement obj) {
        PreparedStatement prep = null;
        boolean res = false;
       	try{
            prep = this.prepare("UPDATE Enseignement SET" +
                                    " master_id= ?, intitule= ?, description= ? where enseignement_id= ?;");
            prep.setLong(1,obj.getMaster().getId());
            prep.setString(2,obj.getIntitule());
            prep.setString(3, obj.getDescription());
            prep.setLong(4, obj.getId());
            prep.execute();
            res = true;
        } catch (SQLException sqle){
            sqle.printStackTrace();
        }finally{
            try{
                if(prep!=null){
                    prep.close();
                }
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
        return res;
    }

        @Override
        public boolean delete(Enseignement obj) {
            PreparedStatement prep = null;
            boolean res = false;
            try{
                prep = prepare("DELETE FROM Enseignement " +
                                    "WHERE Enseignement_id= ?;");
                prep.setLong(1,obj.getId());
                prep.execute();
                res = true;
            } catch (SQLException sqle){
                sqle.printStackTrace();
            }finally{
                try{
                    if(prep!=null){
                        prep.close();
                    }
                }catch(SQLException e){
                   e.printStackTrace();
                }
            }
            return res;
        }

	@Override
	public ResultSet find(long id) {
            try{
                PreparedStatement prep = prepare("SELECT e.Enseignement_ID, e.Intitule, m.Master_ID, m.Nom_Master, e.Description" +
                                                        " FROM Enseignement e, Master m" +
                                                        " WHERE Enseignement_ID = ? AND m.Master_ID = e.Master_ID");
                prep.setLong(1, id);
                if(prep.execute()){
                    return prep.getResultSet();
                }
            } catch (SQLException sqle){
                    sqle.printStackTrace();
            }
            return null;
	}
	
	public ResultSet findEnseignementByMaster(long id_master){
            try{
    		PreparedStatement prep = prepare("SELECT e.Enseignement_ID, e.Intitule, e.Description, m.Master_ID, m.Nom_Master" +
    						" FROM Enseignement e, Master m" +
    						" WHERE m.Master_ID = ? AND m.Master_ID = e.Master_ID");
    		prep.setLong(1, id_master);
                if(prep.execute()){
                    return prep.getResultSet();
                }
            } catch (SQLException sqle){
    		sqle.printStackTrace();
            }
            return null;
	}
}
